package demoMod.moduploader.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import demoMod.gdxform.abstracts.Container;
import demoMod.gdxform.core.FormManager;
import demoMod.gdxform.helpers.FontHelper;
import demoMod.gdxform.interfaces.Element;
import demoMod.gdxform.interfaces.MouseEventSubscriber;
import demoMod.gdxform.ui.GMenuItem;
import demoMod.gdxform.ui.GMenuList;
import demoMod.moduploader.fonts.FontKeys;
import demoMod.moduploader.interfaces.Refreshable;
import demoMod.moduploader.localization.LocalizedStrings;
import demoMod.moduploader.model.ModInfo;
import demoMod.moduploader.model.ModUploadItem;

import java.util.List;

public class ModDisplayItem implements Element, MouseEventSubscriber, Refreshable {
    private String id = "";
    private Container<? extends Element> parent;
    private final BitmapFont font;
    private boolean mouseDown = false;
    private boolean showTitle = true;
    private final ModUploadItem modUploadItem;
    private ModInfo modInfoCache;
    private Texture modCoverCache;

    public ModDisplayItem(BitmapFont font, ModUploadItem modUploadItem) {
        this.font = font;
        this.modUploadItem = modUploadItem;
        refresh();
    }

    public ModUploadItem getModUploadItem() {
        return modUploadItem;
    }

    public ModInfo getModInfoCache() {
        return modInfoCache;
    }

    public boolean showTitle() {
        return showTitle;
    }

    public void setShowTitle(boolean showTitle) {
        this.showTitle = showTitle;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public void update() {

    }

    @Override
    public Container<? extends Element> getParent() {
        return parent;
    }

    @Override
    public void setParent(Container<? extends Element> parent) {
        this.parent = parent;
    }

    @Override
    public float getX(boolean isAbsolute) {
        return getParent() != null ? getParent().getX(isAbsolute) : 0;
    }

    @Override
    public float getY(boolean isAbsolute) {
        return getParent() != null ? getParent().getY(isAbsolute) : 0;
    }

    @Override
    public void setX(float v) {

    }

    @Override
    public void setY(float v) {

    }

    @Override
    public float getWidth() {
        return getParent() != null ? getParent().getWidth() : Gdx.graphics.getWidth();
    }

    @Override
    public float getHeight() {
        return getParent() != null ? getParent().getHeight() : Gdx.graphics.getHeight();
    }

    @Override
    public void setWidth(float v) {

    }

    @Override
    public void setHeight(float v) {

    }

    @Override
    public Color getBackground() {
        return null;
    }

    @Override
    public void setBackground(Color color) {

    }

    @Override
    public boolean isResizable() {
        return false;
    }

    @Override
    public void setResizable(boolean b) {

    }

    @Override
    public boolean enabled() {
        return true;
    }

    @Override
    public void setEnabled(boolean b) {

    }

    @Override
    public boolean visible() {
        return true;
    }

    @Override
    public void setVisible(boolean b) {

    }

    @Override
    public void dispose() {
        if (modCoverCache != null) {
            modCoverCache.dispose();
            modCoverCache = null;
        }
    }

    @Override
    public void refresh() {
        this.modInfoCache = modUploadItem.generateModInfo();
        refreshCover();
    }

    private void refreshCover() {
        if (modCoverCache != null) {
            modCoverCache.dispose();
        }
        modCoverCache = new Texture(Gdx.files.absolute(modUploadItem.getModImage().getAbsolutePath()));
    }

    @Override
    public boolean clickDown(int screenX, int screenY, int button) {
        mouseDown = screenX > getX(true) && screenX < getX(true) + getWidth() && Gdx.graphics.getHeight() - screenY > getY(true)
                && Gdx.graphics.getHeight() - screenY < getY(true) + getHeight();
        if (mouseDown && button == Input.Buttons.RIGHT) {
            GMenuList menuList = new GMenuList();
            menuList.setWidth(150.0F);
            menuList.setX(screenX);
            menuList.setY(Gdx.graphics.getHeight() - screenY);

            List<String> strings = LocalizedStrings.getStrings("ModDisplayItem");

            GMenuItem reloadCover = new GMenuItem(menuList.getWidth(), menuList.getMenuItemHeight(), strings.get(0), FontHelper.getFont(FontKeys.SIM_HEI_14)) {
                @Override
                public void onClick() {
                    refreshCover();
                }
            };
            reloadCover.setSplitLine(false);
            menuList.addElement(reloadCover);

            FormManager.getInstance().addContainer(menuList);
            FormManager.getInstance().moveToTop(menuList);
        }
        if (mouseDown && button == Input.Buttons.LEFT) {
            FormManager.getInstance().getEventHooks().setCurrentFocusedElement(new ModInfoWindow(FontHelper.getFont(FontKeys.SIM_HEI_14), this));
        }
        return false;
    }

    @Override
    public boolean clickUp(int i, int i1, int i2) {
        if (mouseDown) {
            mouseDown = false;
            return true;
        }
        return false;
    }

    @Override
    public boolean mouseDragged(int i, int i1) {
        return false;
    }

    @Override
    public boolean mouseMoved(int i, int i1) {
        return false;
    }

    @Override
    public boolean scrolled(int i) {
        return false;
    }

    @Override
    public boolean moveToElementBorder(Element element) {
        return false;
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setColor(Color.WHITE);
        sb.draw(modCoverCache, getX(true) + getWidth() * 0.1F, getY(true) + getHeight() * 0.1F, getWidth() * 0.8F, getHeight() * 0.8F);
        if (showTitle) {
            FontHelper.renderFontCentered(sb, this.font, modInfoCache.title, getX(true) + getWidth() / 2.0F, getY(true) + this.font.getLineHeight() / 2.0F, Color.WHITE);
        }
    }
}
